﻿using Swashbuckle.Swagger;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Configuration;
using System.Web.Http;
using TiTi.tea.Api.Models;
using TiTi.tea.BLL;
using TiTi.tea.Comm;
using TiTi.tea.Model;

namespace TiTi.tea.Api.Controllers
{
    [RoutePrefix("api/Material")]
    public class MaterialController : ApiController
    {
        private MaterialBLL bll = new MaterialBLL();

        [Route("")]
        [HttpGet]
        public Result GetAll()
        {
            var data = bll.GetAll();

            return Result.Success(data);
        }
        /// <summary>
        /// 查询单个
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("GetOne")]
        [HttpGet]
        public Result GetOne(int id)
        {
            return Result.Success(bll.GetOne(id));
        }
        /// <summary>
        /// 分页获取
        /// </summary>
        /// <param name="matName"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <returns></returns>
        [Route("GetPage")]
        [HttpGet]
        public Result GetPage(string matName="", int pageSize=10, int pageIndex=1)
        {
            var pagePara = new PagePara(pageSize, pageIndex);

            var data = bll.GetAll(matName, pagePara);

            return Result.Success(new { data, total = pagePara.Total });
        }

        /// <summary>
        /// 修改小料
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Update")]
        [HttpPut]
        public Result Update(Material param)
        {
            if (param.Id==0)
            {
                return Result.Error("id不能为空");
            }
            else
            {
                Material ma = bll.GetOne(param.Id);
                ma.MatName = param.MatName;
                ma.Price = param.Price;
                ma.IsDelete = param.IsDelete;
                ma.Status = param.Status;

                bool num=bll.Update(ma);
                if (num)
                {
                    return Result.Success();
                }
                else
                {
                    return Result.Error("修改失败");
                }
            }
        }
        /// <summary>
        /// 添加方法
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public Result Add(Material param) 
        {
            if (bll.Add(param))
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("添加失败");
            }
        }
        /// <summary>
        /// 删除方法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Result Delete(int id) 
        {
            Material ma=bll.GetOne(id);
            if (ma == null)
            {
                return Result.Error("该对象不存在");
            }
            ma.IsDelete = 1;
            bool num=bll.Update(ma);
            if (num) 
            {
                return Result.Success();
            }
            else
            {
                return Result.Error("删除失败");
            }
        }
    }
}